package com.link.base.base.user.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.link.base.base.user.model.User;
import com.link.base.base.user.model.UserExperience;
import com.link.base.base.user.service.UserExperienceService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 快速体验
 *
 * @author ouyangzhenqiang
 * @date 2018/12/27
 */
@Controller
@RequestMapping("/link/userExperience")
public class UserExperienceController extends BasicController<UserExperience> {

    @Resource
    private UserExperienceService userExperienceService;

    @Override
    public BasicService<UserExperience> getBasicService() throws Exception {
        return userExperienceService;
    }

    /**
     * 发送体验短信验证码
     *
     * @author ouyangzhenqiang
     * @date 2018/12/27
     */
    @RequestMapping(value = "/sendSMSCode", method = {RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> sendSMSCode(@JsonParam UserExperience record,
                                     HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            if (StringUtils.isBlank(record.getMobilePhone())) {
                throw new ServiceException("PUBLIC-009");
            }
            String sendTimeInterval = userExperienceService.sendSMSCode(record);
            result.put("success", true);
            result.put("result", sendTimeInterval);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 立即体验
     *
     * @author ouyangzhenqiang
     * @date 2018/12/27
     */
    @RequestMapping(value = "/experience", method = {RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> experience(@JsonParam UserExperience record,
                                     HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            if (StringUtils.isBlank(record.getMobilePhone())) {
                throw new ServiceException("PUBLIC-009");
            }
            if (StringUtils.isBlank(record.getSmsCode())) {
                throw new ServiceException("PUBLIC-039");
            }
            if (StringUtils.isBlank(record.getAppType())) {
                throw new ServiceException("USER-044");
            }
            result = userExperienceService.experienceLogin(record);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 免密码登录,提供给微信工程调用
     *
     * @author 黄龙建
     * @date 2020年06月12日
     */
    @RequestMapping(value = "/loginWithOutPassword", method = {RequestMethod.POST})
    @ResponseBody
    public Map<String, Object> loginWithOutPassword(@RequestBody User user, HttpSession session,
                                                    HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<>();
        try {
            UserExperience userExperience = userExperienceService.loginWithOutPassword(user);
            result.put("success", true);
            result.put("result", userExperience);
        } catch (Exception e) {
            result.put("success", false);
            result.put("result", e.getMessage());
            LCLogger.withException(e);
        }
        return result;
    }
}
